home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 49 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  53 lines

  1. Path: feed.umontreal.ca!usenet
  2. From: danielv@lagrange.crt (Daniel Villeneuve)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Alignment of malloc()
  5. Date: 08 Jan 1996 14:57:11 GMT
  6. Organization: Universite de Montreal
  7. Distribution: world
  8. Message-ID: <DANIELV.96Jan8095711@lagrange.crt>
  9. References: <DKDA7D.Kw7@midway.uchicago.edu> <j66Sx*FRe@yaps.rhein.de>
  10.     <DKKHCH.L6r@midway.uchicago.edu> <4ccbdb$5v6@fg70.rz.uni-karlsruhe.de>
  11.     <DKo8ns.8A9@midway.uchicago.edu>
  12. Reply-To: danielv@crt.umontreal.ca
  13. NNTP-Posting-Host: gate.crt.umontreal.ca
  14. In-reply-to: Michael Spertus's message of Thu, 4 Jan 1996 19:26:16 GMT
  15.  
  16.  
  17. Michael Spertus<mps@geodesic.com> writes:
  18.  
  19. > Therefore, any code using floats must align all floating point malloc()
  20. > to avoid running with abysmal efficiency on many of the most popular
  21. > C/C++ compilers.
  22.  
  23. This does solve the problem only for direct allocation of floats.  If the
  24. compiler is unaware of performance hits when aligning floats on 4-byte
  25. boundaries, it will only pad its structures on 4-byte boundaries.  So the
  26. following structure:
  27.  
  28. struct {
  29.   double d1;
  30.   long   l1;
  31.   double d2;
  32.   long   l2;
  33. };
  34.  
  35. will be packed (no padding) (assuming sizeof(long) == 4) and access to
  36. either d1 or d2 will be misaligned, no matter the alignment of the struct.
  37.  
  38. This problem may be solved if all doubles are pushed at the beginning of
  39. the structure, but there is no hint in the Standard why one should do so.
  40.  
  41. This remark was just to say that the alignment problem is not really a
  42. problem with software, such as the semantics of malloc(), but a problem
  43. with the machine architecture, because the struct above will have to be
  44. translated with the same offsets by every compiler on a given architecture
  45. (this is not a Standard's requirement, but a strong marketing one).
  46.  
  47. -----------------------------------------------------------------------
  48. Daniel Villeneuve                       Tel: +1 514 340 6046
  49. Graduate student in Operations Research Fax: +1 514 340 5665
  50. GERAD/Mathematiques Appliquees          Email: danielv@crt.umontreal.ca
  51. Ecole Polytechnique de Montreal
  52.  
  53.